<template>
  <div class="container">
    <el-dialog :title="dialog.dialogType === 'edit' ? '修改仓库' : '创建仓库'" :visible.sync="dialog.centerDialogVisible"
      width="30%" center :lock-scroll="true" @close="hideDialog">
      <el-form ref="formData" :rules="rules" :model="regions" label-width="100px" label-position="left">
        <el-form-item label="仓库：" prop="buyername">
          <el-input v-model="regions.name" placeholder="请输入仓库" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import { Warehousestore, Warehouseupdate } from "@/api/warehouse/warehouse";
  import store from "@/store";
  export default {
    props: {
      dialog: Object,
      regions: Object,
    },
    data() {
      return {
        rules: {
          name: [{ required: true, message: "仓库不能为空", trigger: "blur" }],
        },
      };
    },

    methods: {
      hideDialog() {
        this.dialog.centerDialogVisible = false;
        this.$refs["formData"].resetFields();
      },

      //提交
      confirm() {
        this.$refs["formData"].validate((valid) => {
          if (valid) {
            let data = {
              house_name: this.regions.name,
            };
            if (this.dialog.dialogType == "edit") {
              data.id = this.regions.id;
              Warehouseupdate(data)
                .then((res) => {
                  if (res.code == 0) {
                    this.$message({
                      message: res.msg,
                      type: "success",
                    });
                    this.hideDialog();
                    this.$emit("upData");
                  } else {
                    this.$message({
                      message: res.msg,
                      type: "error",
                    });
                  }
                })
                .catch((err) => {
                  console.log(err);
                });
            } else {
              Warehousestore(data)
                .then((res) => {
                  if (res.code == 0) {
                    this.$message({
                      message: res.msg,
                      type: "success",
                    });
                    this.hideDialog();
                    this.$emit("upData");
                  } else {
                    this.$message({
                      message: res.msg,
                      type: "error",
                    });
                  }
                })
                .catch((err) => {
                  console.log(err);
                });
            }
          }
        });
      },
      //取消
      cancel() {
        this.hideDialog();
      },
    },
  };
</script>